package ofmybatis.demo.controller;


import ofmybatis.demo.bean.Components;
import ofmybatis.demo.bean.SpringBeanUtil;
import ofmybatis.demo.dao.back.PeopleMapper;
import ofmybatis.demo.pojo.People;
import ofmybatis.demo.pojo.UserInfo;
import ofmybatis.demo.service.UserInfoService;
import ofmybatis.demo.transaction.TestTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value={"/user"})
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private PeopleMapper peopleMapper;

    @RequestMapping(value = {"/findUser"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public UserInfo getUserInfo(){
        return userInfoService.getUserInfo();
    }

    @RequestMapping(value = {"/findPeople"}, method = RequestMethod.GET)
    public People getPeople(){
        return peopleMapper.findPeople();
    }

    @Resource
    private JdbcTemplate jdbcTemplate;

    /***
     * 测试jdbcTemplate . queryForMap() 方法 返回的结构
     */
    @RequestMapping(value = {"/testJdbc"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public  Map<String,Object> testJdbcqueryForMap(){
        String sql ="select * from user WHERE ID = 2 ";
        Map<String,Object> map = jdbcTemplate.queryForMap(sql);
        System.out.println(map.toString());  // {id=2, address=zhonghua shanghai, username=kangmjs, password=Aa123454t}
        return map;
    }

    @RequestMapping(value = {"/testJdbc2"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
    public List<Map<String,Object>> testJdbcqueryForMap2(){
        String sql ="select * from user";
        List<Map<String,Object>> map = jdbcTemplate.queryForList(sql);
        System.out.println(map.toString());
        return map;
    }

    @RequestMapping (value = {"/testds"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET  )
    public void addRe() {
        Components components = Components.getInstance();
        components.test();

    }

    @Autowired
    private TestTransaction testTransaction ;


    @RequestMapping (value = {"/testds2"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET  )
    public void addRe2() {

        testTransaction.test2();

    }


}












